package com.hqd.ch03.v13.reader.xml.resolver;

import com.hqd.ch03.v13.io.support.ClassPathResource;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.net.URL;

public class BeansDtdResolver implements EntityResolver {

    /**
     * TODO spring这里无法解析自定义dtd
     *
     * @param publicId
     * @param systemId
     * @return
     * @throws SAXException
     * @throws IOException
     */
    @Override
    public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
        URL url = new URL(systemId);
        ClassPathResource resource = new ClassPathResource(url.getPath().substring(1));
        InputSource source = new InputSource(resource.getInputStream());
        source.setPublicId(publicId);
        source.setSystemId(systemId);
        return source;
    }
}
